﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSNhanXet : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();

        public void EditNX(int hdId, DateTime ngay, string noiDung)
        {
            List<NSNhanXet> lst = (List<NSNhanXet>)HttpContext.Current.Session["NX"];
            NSNhanXet pc = lst.Where(a => a.NhanXetId.Equals(hdId)).SingleOrDefault();
            pc.NgayThangNhanXet = ngay;
            pc.NoiDung = noiDung;
        }
        public void DeleteNX(int hdId)
        {
            List<NSNhanXet> lst = (List<NSNhanXet>)HttpContext.Current.Session["NX"];
            NSNhanXet pc = lst.Where(a => a.NhanXetId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllNXByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSNhanXets
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSNhanXets.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateNX_NhanVien(int nhanVienId, List<NSNhanXet> obj)
        {

            var queryNBLByNhanVienId = (from a in db.NSNhanXets
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSNhanXets.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSNhanXet pc in obj)
            {
                NSNhanXet item = new NSNhanXet();
                CopyNSNhanXet(pc, item);
                db.NSNhanXets.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSNhanXet(NSNhanXet src, NSNhanXet des)
        {
            des.NgayThangNhanXet = src.NgayThangNhanXet;
            des.NoiDung = src.NoiDung;
            des.NhanVienID = src.NhanVienID;
        }
        public List<NSNhanXet> LoadNXByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSNhanXets
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NhanXetId descending
                         select a).ToList();

            return query;

        }
        public NSNhanXet GetNhanXet(int nhanVienId, DateTime ngay, string noiDung)
        {
            var db = new DataClassesDataContext();
            NSNhanXet hd = new NSNhanXet();
            hd.NhanXetId = 0 - DateTime.Now.Second;
            hd.NgayThangNhanXet = ngay;
            hd.NoiDung = noiDung;
            hd.NhanVienID = nhanVienId;
           
            return hd;
        }
    }
}